import React, {Component} from 'react';
import reactDom from 'react-dom';

import Uploader from '../../../../../public/components/UploaderImgWithBase64';

import {
    toast
} from 'nc-lightapp-front';

import language from '../../../language';
import { getColor } from '../../../../../../hrpub/common/utils/utils';

const imageList = ['.png', '.jpg', '.gif', '.jpeg'];

export default function() {
    
    if(document.getElementById('photoDiv')) {
        return;
    }

    const {
        form: {
            setFormItemsVisible
        },
        imageData,
        MultiInit
    } = this.props;
    
    
    setFormItemsVisible('bd_psndoc', {
        photo: false
    });
    
    let oFormBox = document.getElementById('form_bd_psndoc').children[0];
    let oDiv = document.createElement('div');
    oDiv.id = 'photoDiv';
    oDiv.style.float = 'right';
    oDiv.style.marginRight = '137px';
    oDiv.style.backgroundColor = getColor().bgColor;

    
    oFormBox.insertBefore(oDiv, oFormBox.children[0]);
    
    reactDom.render(<Uploader
        ref={(ref) => {
            this.props.setUpLoaderRef(ref);
        }}
        onUpload={(result, file) => {
            let filename = file.name;
            let fileExt = filename.substring(filename.lastIndexOf('.'));

            if(imageList.includes(fileExt.toLowerCase())) {
                this.props.updateState({
                    imageData: result
                })
            }
            else {
                toast({
                    color: 'danger',
                    content: language['hi6007-000177'] /* 上传格式不正确 */
                });

                return false;
            }
        }}
        value={imageData}
        MultiInit={MultiInit}
    />, oDiv);
}
